package com.ruoyi.web.controller.lingshui.decisupport;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.supervise.domain.decisupport.ComparisonMap;
import com.ruoyi.supervise.domain.decisupport.ConstructionProject;
import com.ruoyi.supervise.domain.decisupport.MyBigDataProZS;
import com.ruoyi.supervise.domain.decisupport.ProportionOfProjectType;
import com.ruoyi.supervise.domain.decisupport.TwoGuarantees;
import com.ruoyi.supervise.service.decisupport.ComparisonMapService;
import com.ruoyi.supervise.service.decisupport.ConstructionProjectService;
import com.ruoyi.supervise.service.decisupport.MyBigDataProZSService;
import com.ruoyi.supervise.service.decisupport.ProportionOfProjectTypeService;
import com.ruoyi.supervise.util.DateUtils;

import cn.hutool.core.date.DateUtil;

/**
 * @ClassName: IndexController
 * @Description: 建设工程开工情况controller
 * @Author wtj
 * @DateTime 2020年1月14日 下午4:40:47
 */

@Controller
@RequestMapping("/buildIndex")
public class IndexController {
	
	
	@Autowired
	private ConstructionProjectService constructionProjectService;
	
	@Autowired
	private ProportionOfProjectTypeService proportionOfProjectTypeService;
	
	@Autowired
	private MyBigDataProZSService myBigDataProZSService;
	
	@Autowired
	private ComparisonMapService comparisonMapService;
	
	/**
	 * @Title: hello
	 * @Description: 
	 * @Author wtj
	 * @DateTime 2020年1月13日 上午10:37:39
	 * @return
	 */
	@GetMapping("/analysis")
	public String hello() {
		return "decisionSupport/constructionProject/page.html";
	}
	
	/**
	 * @Title: indexAllTable
	 * @Description: 获取详细数据的table
	 * @Author wtj
	 * @DateTime 2020年1月14日 下午4:39:47
	 * @param cp
	 * @return
	 */
	@GetMapping("/indexalltable")
	@ResponseBody
	public List<ConstructionProject> indexAllTable(ConstructionProject cp)
	{
//		cp.setTimeNowBegin("2018-08");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-08");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		ConstructionProject constrPro=processingTime(cp);
		//获取出详细数据表格的数据和左上角开工项目总览数据
		List<ConstructionProject> allTable=constructionProjectService.selectAllTable(constrPro);
		ConstructionProject allProvinceTable=constructionProjectService.selectAllProvinceTable(constrPro);
		List<ConstructionProject> all=new ArrayList<ConstructionProject>();
		for(int i=0;i<allTable.size()+1;i++)
		{
			if(i==0)
			{
				all.add(allProvinceTable);
			}else
			{
				all.add(allTable.get(i-1));
			}
		}
		return all;
	}
	
	/**
	 * @Title: twoGuarantees
	 * @Description:获取右下角"两个确保"成果的数据 
	 * @Author wtj
	 * @DateTime 2020年1月14日 下午4:41:29
	 * @param cp
	 * @return
	 */
	@GetMapping("/twoguarantees")
	@ResponseBody
	public TwoGuarantees twoGuarantees(ConstructionProject cp)
	{
		//cp.setIsPrivateConstruction("1");
		//cp.setTimeNowBegin("2018-05");
		String nowYearStr=" ";
		int nowYearInt=0;
		int year=0;
		if(StringUtils.isAnyBlank(cp.getTimeNowBegin()))
		{
			nowYearInt=DateUtil.thisYear();
			nowYearStr=String.valueOf(nowYearInt);
		}else
		{
			Date dateNowBegin = DateUtil.parse(cp.getTimeNowBegin(), "yyyy-MM");
			nowYearInt=DateUtil.year(dateNowBegin);
			nowYearStr=String.valueOf(nowYearInt);
		}
		boolean isLeapYear=DateUtil.isLeapYear(nowYearInt);
		if(isLeapYear)
		{
			year=366;
		}else
		{
			year=365;
		}
		DecimalFormat df = new DecimalFormat("0.00");
		TwoGuarantees tgData=new TwoGuarantees();
		ConstructionProject cpBefor=new ConstructionProject();
		cpBefor.setProjectCityNum("陵水县");
		cpBefor.setTimeNowBegin(nowYearStr+"-01-01 00:00:00");
		cpBefor.setTimeNowEnd(nowYearStr+"-08-15 23:59:59");
		cpBefor.setIsPrivateConstruction(cp.getIsPrivateConstruction());
		ConstructionProject cpAfter=new ConstructionProject();
		cpAfter.setProjectCityNum("陵水县");
		cpAfter.setTimeNowBegin(nowYearStr+"-08-16 00:00:00");
		cpAfter.setTimeNowEnd(nowYearStr+"-12-31 23:59:59");
		cpAfter.setIsPrivateConstruction(cp.getIsPrivateConstruction());
		//发证个数，"两个确保"前
		int numBeforInt=constructionProjectService.countNum(cpBefor);
		tgData.setNumBefor(String.valueOf(numBeforInt));
		//发证个数，"两个确保"后
		int numAfterInt=constructionProjectService.countNum(cpAfter);
		tgData.setNumAfter(String.valueOf(numAfterInt));
		//计算"两个确保"前，日均发证个数
	    String numDayAverageBefor = df.format((double) numBeforInt / (year-138));
		//Double numDayAverageBeforDouble=(double) (numBeforInt/(year-138));
		tgData.setNumDayAverageBefor(numDayAverageBefor);
		//计算"两个确保"后，日均发证个数
		String numDayAverageAfter = df.format((double) numAfterInt/(138));
		//Double numDayAverageAfterDouble=(double) (numAfterInt/(138));
		tgData.setNumDayAverageAfter(numDayAverageAfter);
		//计算日均发证个数增长幅度
		if(numDayAverageBefor.equals("0.00") && numDayAverageAfter.equals("0.00"))
		{
			tgData.setNumDayAverageCompare("0.00");
		}else if(numDayAverageBefor.equals("0.00") && !numDayAverageAfter.equals("0.00"))
		{
			tgData.setNumDayAverageCompare("100.00");
		}else
		{
			Double numDayAverageAfterDouble=Double.parseDouble(numDayAverageAfter);
			Double numDayAverageBeforDouble=Double.parseDouble(numDayAverageBefor);
			String numDayAverageCompare=df.format((double) ((numDayAverageAfterDouble-numDayAverageBeforDouble)/numDayAverageBeforDouble)*100);
			//Double numDayAverageCompareDouble=((numDayAverageAfterDouble-numDayAverageBeforDouble)/numDayAverageBeforDouble)*100;
			tgData.setNumDayAverageCompare(numDayAverageCompare);
		}
		//合同金额，"两个确保"前
		Double priceBeforDouble=constructionProjectService.countPrice(cpBefor);
		if(priceBeforDouble==null)
		{
			priceBeforDouble=0.00;
		}
		String priceBefor=df.format((double) priceBeforDouble / 10000);
		tgData.setPriceBefor(priceBefor);
		//合同金额，"两个确保"后
		Double priceAfterDouble=constructionProjectService.countPrice(cpAfter);
		if(priceAfterDouble==null)
		{
			priceAfterDouble=0.00;
		}
		String priceAfter=df.format((double) priceAfterDouble / 10000);
		tgData.setPriceAfter(priceAfter);
		//计算"两个确保"前，日均金额
		String priceDayAverageBefor=df.format((double) (priceBeforDouble/(year-138))/10000);
		//Double priceDayAverageBeforDouble=(priceBeforDouble/(year-138))/10000;
		tgData.setPriceDayAverageBefor(priceDayAverageBefor);
		//计算"两个确保"后，日均金额
		String priceDayAverageAfter=df.format((double) (priceAfterDouble/138)/10000);
		//Double priceDayAverageAfterDouble=(priceAfterDouble/138)/10000;
		tgData.setPriceDayAverageAfter(priceDayAverageAfter);
		//计算日均金额增长幅度
		if(priceDayAverageBefor.equals("0.00") && priceDayAverageAfter.equals("0.00"))
		{
			tgData.setPriceDayAverageCompare("0.00");
		}else if(priceDayAverageBefor.equals("0.00") && !priceDayAverageAfter.equals("0.00"))
		{
			tgData.setPriceDayAverageCompare("100.00");
		}else 
		{
			Double priceDayAverageBeforDouble=Double.parseDouble(priceDayAverageBefor);
			Double priceDayAverageAfterDouble=Double.parseDouble(priceDayAverageAfter);
			String priceDayAverageCompare=df.format((double) ((priceDayAverageAfterDouble-priceDayAverageBeforDouble)/priceDayAverageBeforDouble)*100);
			//Double priceDayAverageCompareDouble=((priceDayAverageAfterDouble-priceDayAverageBeforDouble)/priceDayAverageBeforDouble)*100;
			tgData.setPriceDayAverageCompare(priceDayAverageCompare);
		}
		return tgData;
	}
	
	/**
	 * @Title: compareProType
	 * @Description: 左下角饼图
	 * @Author wtj
	 * @DateTime 2020年1月14日 下午5:19:59
	 * @param cp
	 * @return
	 */
	@GetMapping("/compareprotypeall")
	@ResponseBody
	public List<ProportionOfProjectType> compareProType(ConstructionProject cp)
	{
//		cp.setTimeNowBegin("2018-08");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-08");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		cp.setProjectCityNum("陵水县");
		ConstructionProject cpdata=processingTime(cp);
		List<ProportionOfProjectType> pptList=proportionOfProjectTypeService.AllProportionOfProjectTypeBT(cpdata);
		return pptList;
	}
	
	/**
	 * @Title: startAllNumMonthZST
	 * @Description: 右上角全省开工月度趋势走势图
	 * @Author wtj
	 * @DateTime 2020年1月15日 上午11:56:40
	 * @param cp
	 * @return
	 */
	@GetMapping("/startallnummonthzst")
	@ResponseBody
	public Map<String,Object> startAllNumMonthZST(ConstructionProject cp) {
//		cp.setTimeNowBegin("2018-01");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-01");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		cp.setProjectCityNum("陵水县");
		ConstructionProject cpdata=processingTime(cp);
		List<MyBigDataProZS> allProvinceStartProNumList=myBigDataProZSService.selectAllProNumCPMonthZS(cpdata);
		Map<String,Object> allProvinceStartProNumMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceStartProNumList.size()];
		String[] numTotalArr=new String[allProvinceStartProNumList.size()];
		String[] numTotalCompareArr=new String[allProvinceStartProNumList.size()];
		for(int i=0;i<allProvinceStartProNumList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceStartProNumList.get(i).getMonth());
			numTotalArr[i]=allProvinceStartProNumList.get(i).getTotal();
			numTotalCompareArr[i]=allProvinceStartProNumList.get(i).getCompare();
		}
		allProvinceStartProNumMap.put("month", monthArr);
		allProvinceStartProNumMap.put("numTotalArr", numTotalArr);
		allProvinceStartProNumMap.put("numTotalCompareArr", numTotalCompareArr);
		return allProvinceStartProNumMap;
	}
	
	/**
	 * @Title: startAllMoneyMonthZST
	 * @Description: 右上角全省投资月度趋势走势图
	 * @Author wtj
	 * @DateTime 2020年1月15日 下午4:21:19
	 * @param cp
	 * @return
	 */
	@GetMapping("/startallmoneymonthzst")
	@ResponseBody
	public Map<String,Object> startAllMoneyMonthZST(ConstructionProject cp) {
//		cp.setTimeNowBegin("2018-01");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-01");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		cp.setProjectCityNum("陵水县");
		ConstructionProject cpdata=processingTime(cp);
		List<MyBigDataProZS> allProvinceStartProMoneyList=myBigDataProZSService.selectAllProMoneyCPMonthZS(cpdata);
		Map<String,Object> allProvinceStartProMoneyMap=new HashMap<String,Object>();
		String[] monthArr=new String[allProvinceStartProMoneyList.size()];
		String[] moneyTotalArr=new String[allProvinceStartProMoneyList.size()];
		String[] moneyTotalCompareArr=new String[allProvinceStartProMoneyList.size()];
		for(int i=0;i<allProvinceStartProMoneyList.size();i++)
		{
			monthArr[i] = String.valueOf(allProvinceStartProMoneyList.get(i).getMonth());
			moneyTotalArr[i]=allProvinceStartProMoneyList.get(i).getTotal();
			moneyTotalCompareArr[i]=allProvinceStartProMoneyList.get(i).getCompare();
		}
		allProvinceStartProMoneyMap.put("month", monthArr);
		allProvinceStartProMoneyMap.put("moneyTotalArr", moneyTotalArr);
		allProvinceStartProMoneyMap.put("moneyTotalCompareArr", moneyTotalCompareArr);
		return allProvinceStartProMoneyMap;
	}
	
	/**
	 * @Title: selectAllStartProNumNowAndLastDBT
	 * @Description: 开工数量对比图
	 * @Author wtj
	 * @DateTime 2020年1月15日 下午4:21:49
	 * @param cp
	 * @return
	 */
	@GetMapping("/selectallstartpronumnowandlastdbt")
	@ResponseBody
	public Map<String,Object> selectAllStartProNumNowAndLastDBT(ConstructionProject cp) 
	{
//		cp.setTimeNowBegin("2018-01");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-01");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		if("1".equals(cp.getEngineeringType()))
		{
			cp.setEngineeringType("房屋建筑工程");
		}else if("2".equals(cp.getEngineeringType()))
		{
			cp.setEngineeringType("市政工程");
		}else 
		{
			cp.setEngineeringType(null);
		}
		//ConstructionProject cpdata=processingTime(cp);
		List<ComparisonMap> numList=comparisonMapService.selectAllStartProNumNowAndLastDBT(cp);
		Map<String,Object> numMap=new HashMap<String,Object>();
		String[] projectCityArr=new String[numList.size()];
		String[] numTotalNowArr=new String[numList.size()];
		String[] numTotalLastArr=new String[numList.size()];
		for(int i=0;i<numList.size();i++)
		{
			projectCityArr[i] = numList.get(i).getProjectCityNum();
			numTotalNowArr[i]=numList.get(i).getNow();
			numTotalLastArr[i]=numList.get(i).getLast();
		}
		numMap.put("projectCityArr", projectCityArr);
		numMap.put("numTotalNowArr", numTotalNowArr);
		numMap.put("numTotalLastArr", numTotalLastArr);
		return numMap;
	}
	
	/**
	 * @Title: selectAllStartProMoneyNowAndLastDBT
	 * @Description: 投资金额对比图
	 * @Author wtj
	 * @DateTime 2020年1月15日 下午4:22:09
	 * @param cp
	 * @return
	 */
	@GetMapping("/selectallstartpromoneynowandlastdbt")
	@ResponseBody
	public Map<String,Object> selectAllStartProMoneyNowAndLastDBT(ConstructionProject cp) 
	{
//		cp.setTimeNowBegin("2018-01");
//		cp.setTimeNowEnd("2020-01");
//		cp.setTimeLastBegin("2017-01");
//		cp.setTimeLastEnd("2019-01");
//		cp.setIsPrivateConstruction("0");
		if("1".equals(cp.getEngineeringType()))
		{
			cp.setEngineeringType("房屋建筑工程");
		}else if("2".equals(cp.getEngineeringType()))
		{
			cp.setEngineeringType("市政工程");
		}else 
		{
			cp.setEngineeringType(null);
		}
		//ConstructionProject cpdata=processingTime(cp);
		List<ComparisonMap> moneyList=comparisonMapService.selectAllStartProMoneyNowAndLastDBT(cp);
		Map<String,Object> moneyMap=new HashMap<String,Object>();
		String[] projectCityArr=new String[moneyList.size()];
		String[] moneyTotalNowArr=new String[moneyList.size()];
		String[] moneyTotalLastArr=new String[moneyList.size()];
		for(int i=0;i<moneyList.size();i++)
		{
			projectCityArr[i] = moneyList.get(i).getProjectCityNum();
			moneyTotalNowArr[i]=moneyList.get(i).getNow();
			moneyTotalLastArr[i]=moneyList.get(i).getLast();
		}
		moneyMap.put("projectCityArr", projectCityArr);
		moneyMap.put("numTotalNowArr", moneyTotalNowArr);
		moneyMap.put("numTotalLastArr", moneyTotalLastArr);
		return moneyMap;
	}
	
	public ConstructionProject processingTime(ConstructionProject cp)
	{
		//对时间进行处理
		DateUtils du=new DateUtils();
		Calendar calendarBegin = Calendar.getInstance();
		Calendar calendarEnd = Calendar.getInstance();
		if(StringUtils.isAnyBlank(cp.getTimeNowBegin(),cp.getTimeNowEnd()))
		{
			Date beginDayOfNowYear=du.getBeginDayOfYear();//获取当前年的第一天
			Date endDayOfNowYear=du.getEndDayOfYear();//获取当前年的最后一天
			calendarBegin.setTime(beginDayOfNowYear);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYear=calendarBegin.getTime();//获取特定时间的去年时间
			calendarEnd.setTime(endDayOfNowYear);
			calendarEnd.add(Calendar.YEAR,-1);
			Date endDayOfLastYear=calendarEnd.getTime();//获取特定时间的去年时间
			SimpleDateFormat formatterNow2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			cp.setTimeNowBegin(formatterNow2.format(beginDayOfNowYear));
			cp.setTimeNowEnd(formatterNow2.format(endDayOfNowYear));
			cp.setTimeLastBegin(formatterNow2.format(beginDayOfLastYear));
			cp.setTimeLastEnd(formatterNow2.format(endDayOfLastYear));
		}else
		{	
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date dateNowBegin = DateUtil.parse(cp.getTimeNowBegin(), "yyyy-MM");
			Date dateNowBeginDay=DateUtil.beginOfMonth(dateNowBegin);
			cp.setTimeNowBegin(formatter.format(DateUtil.beginOfMonth(dateNowBeginDay)));
			calendarBegin.setTime(dateNowBeginDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date beginDayOfLastYearMonth=calendarBegin.getTime();
			cp.setTimeLastBegin(formatter.format(DateUtil.beginOfMonth(beginDayOfLastYearMonth)));
			Date dateNowEnd = DateUtil.parse(cp.getTimeNowEnd(), "yyyy-MM");
			Date dateNowEndDay=DateUtil.beginOfMonth(dateNowEnd);
			cp.setTimeNowEnd(formatter.format(DateUtil.endOfMonth(dateNowEndDay)));
			calendarBegin.setTime(dateNowEndDay);
			calendarBegin.add(Calendar.YEAR,-1);
			Date EndDayOfLastYearMonth=calendarBegin.getTime();
			cp.setTimeLastEnd(formatter.format(DateUtil.endOfMonth(EndDayOfLastYearMonth)));
		}
		return cp;
	}
	
	
}
